package com.meichis.yndms.component;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;

import com.meichis.mcsappframework.utils.ScreenUtil;
import com.meichis.mcsappframework.wheel.OnWheelChangedListener;
import com.meichis.mcsappframework.wheel.OnWheelScrollListener;
import com.meichis.mcsappframework.wheel.WheelView;
import com.meichis.mcsappframework.wheel.adapters.ArrayWheelAdapter;
import com.meichis.yndms.R;
import com.meichis.yndms.model.OfficialCity;
import com.meichis.yndms.room.AppDatabase;

import java.util.ArrayList;

/**
 * Created by chenhaifei on 2016/8/11.
 */
public class CityChoicePopView extends PopupWindow implements View.OnClickListener {
    private Context mcontext;
    private SelectedListener mListener;
    WheelView wv_province, wv_city, wv_area;

    private ArrayList<OfficialCity> provincelist, citylist, arealist;
    private Boolean scrolling = false;
    private int curprovince, curcity, curarea;

    public interface SelectedListener {

        void setValue(String Name, int ID);
    }

    public CityChoicePopView(Context context, int city, SelectedListener listener) {
        super();

        mcontext = context;
        mListener = listener;
        curarea = city;
        if (city > 0) {
            OfficialCity areacity = AppDatabase.getInstance().officialCityDao().find(city);
            OfficialCity ofcity = AppDatabase.getInstance().officialCityDao().find(areacity.getSuperID());
            curprovince = ofcity.getSuperID();
            curcity = ofcity.getID();
        }
        initViews(context);
        setWidth(ScreenUtil.getScreenWidth());//设置popwindow的宽为屏幕的实际像素宽
        setHeight(ScreenUtil.dip2px( 240));
        setFocusable(true);
        setBackgroundDrawable(new BitmapDrawable());
        setOutsideTouchable(true);
        setAnimationStyle(R.style.AnimBottom);
    }

    private void initViews(Context context) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.layout_citychoice_popview, null);
        setContentView(contentView);
        contentView.findViewById(R.id.bt_ok).setOnClickListener(this);
        wv_province = (WheelView) contentView.findViewById(R.id.wv_province);
        wv_city = (WheelView) contentView.findViewById(R.id.wv_city);
        wv_area = (WheelView) contentView.findViewById(R.id.wv_area);

        wv_province.setVisibleItems(5);
        wv_province.setCyclic(true);
        wv_city.setVisibleItems(5);
        wv_city.setCyclic(true);
        wv_area.setVisibleItems(5);
        wv_area.setCyclic(true);

        provincelist = (ArrayList<OfficialCity>) AppDatabase.getInstance().officialCityDao().findBySuperID(1);

        if (provincelist.size() == 1)
            wv_province.setCyclic(false);

        wv_province.addChangingListener(changelistener);
        wv_city.addChangingListener(changelistener);
        wv_province.addScrollingListener(scrolllistener);
        wv_city.addScrollingListener(scrolllistener);

        updateCities(wv_province, provincelist);
        if (provincelist.size() == 1) {
            citylist = (ArrayList<OfficialCity>) AppDatabase.getInstance().officialCityDao().findBySuperID(provincelist.get(0).getID());
            updateCities(wv_city, citylist);
        }
    }

    private OnWheelChangedListener changelistener = new OnWheelChangedListener() {

        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            if (scrolling)
                return;
            switch (wheel.getId()) {
                case R.id.wv_province:
                    citylist = (ArrayList<OfficialCity>) AppDatabase.getInstance().officialCityDao().findBySuperID(provincelist.get(newValue).getID());
                    updateCities(wv_city, citylist);
                    break;
                case R.id.wv_city:
                    arealist = (ArrayList<OfficialCity>) AppDatabase.getInstance().officialCityDao().findBySuperID(citylist.get(newValue).getID());
                    updateCities(wv_area, arealist);
                    break;
                case R.id.wv_area:
                    break;
            }

        }
    };
    private OnWheelScrollListener scrolllistener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {
            scrolling = true;
        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            scrolling = false;
            switch (wheel.getId()) {
                case R.id.wv_province:
                    citylist = (ArrayList<OfficialCity>) AppDatabase.getInstance().officialCityDao().findBySuperID(provincelist.get(wv_province.getCurrentItem()).getID());
                    updateCities(wv_city, citylist);
                    break;
                case R.id.wv_city:
                    arealist = (ArrayList<OfficialCity>) AppDatabase.getInstance().officialCityDao().findBySuperID(citylist.get(wv_city.getCurrentItem()).getID());
                    updateCities(wv_area, arealist);
                    break;
                case R.id.wv_area:
                    break;
            }

        }
    };

    private void updateCities(WheelView city, ArrayList<OfficialCity> pcitylist) {
        ArrayWheelAdapter<OfficialCity> adapter = new ArrayWheelAdapter<OfficialCity>(mcontext, pcitylist, "Name");
        adapter.setTextSize(16);
        city.setViewAdapter(adapter);
        int curitem = pcitylist.size() / 2;

        int olditem = 0;
        if (curarea != 0) {
            switch (city.getId()) {
                case R.id.wv_province:
                    olditem = curprovince;
                    break;
                case R.id.wv_city:
                    olditem = curcity;
                    break;
                case R.id.wv_area:
                    olditem = curarea;
                    break;
            }
        }
        for (int i = 0; i < pcitylist.size(); i++) {
            if (pcitylist.get(i).getID() == olditem) {
                curitem = i;
                break;
            }
        }
        if (pcitylist.size() == 1)
            curitem = 0;
        city.setCurrentItem(curitem);
    }

    @Override
    public void onClick(View v) {
        this.dismiss();
        switch (v.getId()) {
            case R.id.bt_ok:
                if (arealist == null)
                    return;
                String cityname = provincelist.get(wv_province.getCurrentItem()).getName() + " " + citylist.get(wv_city.getCurrentItem()).getName();

                int CityID = citylist.get(wv_city.getCurrentItem()).getID();
                if (arealist != null && arealist.size() > 0) {
                    cityname += " " + arealist.get(wv_area.getCurrentItem()).getName();
                    CityID = arealist.get(wv_area.getCurrentItem()).getID();
                }
                mListener.setValue(cityname, CityID);
                break;
        }
    }

}
